Mapping counties by area!

library(lwgeom)
## Linking to liblwgeom 2.5.0dev r16016, GEOS 3.6.1, proj.4 4.9.3
library(maps)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
library(raster)
## Loading required package: sp
library(tmap)
library(micromap)
## Loading required package: maptools
## Checking rgeos availability: TRUE
## Loading required package: RColorBrewer
## Loading required package: rgdal
## rgdal: version: 1.2-20, (SVN revision 725)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/hared/Documents/R/win-library/3.4/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/hared/Documents/R/win-library/3.4/rgdal/proj
##  Linking to sp version: 1.2-7
library(ggrepel)
## Loading required package: ggplot2
library(ggmap)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
counties <- st_as_sf(map('county', plot = F, fill = T)) #same fucntion before, but here we are just specifys county for the, the fill is saying WHEN you plot it fill it!


ggplot() +
  geom_sf(data = counties)

area <- st_area(counties)
head(area)
## Units: m^2
## [1] 1489347617 4107738938 2331593415 1543364095 1634237932 1614622407
class(area)
## [1] "units"
area <-  as.numeric(area)
area <- area / 1e6

counties$area <-  area
head(counties)
## Simple feature collection with 6 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -88.01778 ymin: 30.24071 xmax: -85.06131 ymax: 34.2686
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##                         geometry              ID     area
## 1 MULTIPOLYGON (((-86.50517 3... alabama,autauga 1489.348
## 2 MULTIPOLYGON (((-87.93757 3... alabama,baldwin 4107.739
## 3 MULTIPOLYGON (((-85.42801 3... alabama,barbour 2331.593
## 4 MULTIPOLYGON (((-87.02083 3...    alabama,bibb 1543.364
## 5 MULTIPOLYGON (((-86.9578 33...  alabama,blount 1634.238
## 6 MULTIPOLYGON (((-85.66866 3... alabama,bullock 1614.622
ggplot() + 
  geom_sf(data = counties, aes(fill= area)) +
  scale_fill_distiller(palette = 'PuOr')

Now we are moving to #plotly

#scatter plots
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
## 
##     wind
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:micromap':
## 
##     subplot
## The following object is masked from 'package:raster':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
p
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
#can easily convert ggplot to plotly

p <- ggplot(data = iris, aes(x = Petal.Width, fill = Species)) + 
  geom_histogram()
ggplotly(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#3D!
plot_ly(z = ~volcano, type = 'surface')
#now here is an example

states <- st_as_sf(map('state', plot = F, fill = T))
area <- st_area(states)
area <- as.numeric(area) / (1000^2)
states$area <- area
head(states)
## Simple feature collection with 6 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.3834 ymin: 30.24071 xmax: -71.78015 ymax: 42.04937
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##                         geometry          ID      area
## 1 MULTIPOLYGON (((-87.46201 3...     alabama 132961.79
## 2 MULTIPOLYGON (((-114.6374 3...     arizona 294168.62
## 3 MULTIPOLYGON (((-94.05103 3...    arkansas 138290.04
## 4 MULTIPOLYGON (((-120.006 42...  california 405129.77
## 5 MULTIPOLYGON (((-102.0552 4...    colorado 270611.14
## 6 MULTIPOLYGON (((-73.49902 4... connecticut  13077.24
plot_ly(states)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
plot_ly(states, split = ~ID, color = ~ area, showlegend = F, alpha = 1) #spilt - what are the objects that we want it to find to get color, if we hadnt it wont know how to apply color... nd makes the hover to ID
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
plot_ly(states, split = ~ID, color = ~ area, colors = 'PiYG', showlegend = F, alpha = 1) #can change based on colors
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter

Another cool thing!

Linking two graphs so the highlighting in one plot you can see the spatial position in another plot! think like waht tomasz was doing.

library(crosstalk) #this is a part of ployly but needs to be loaded

# create a shareddata object from states
sts <- SharedData$new(states) #need to do this to create new 

#make the two plots you will link together

# state map, we're not mapping area anymore
state_map <- plot_ly(sts) %>%
  highlight(persistent = T, dynamic = T) #this is the highlight fucntion  (persisent does the same as shift so recommednded to do false)
## Adding more colors to the selection color palette.
## We recommend setting `persistent` to `FALSE` (the default) because persistent selection mode can now be used by holding the shift key (while triggering the `on` event).
# histogram of area
state_hist <- plot_ly(sts, x = ~ area) %>%
  add_histogram(xbins = list(start = 0, end = 1e6, size = 1e4)) %>%
  layout(barmode = "overlay") %>%
  highlight(on = "plotly_selected", persistent = TRUE)
## We recommend setting `persistent` to `FALSE` (the default) because persistent selection mode can now be used by holding the shift key (while triggering the `on` event).
bscols(
  state_hist,
  state_map
)
## Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
# load sp library for meuse data
library(sp)
data(meuse)

# create sf object for meuse
dat <- st_as_sf(meuse, coords = c('x', 'y'), crs = "+init=epsg:28992")

# create a shareddata object for meuse
datshr <- SharedData$new(dat)

# scatterplot
meuse_sct <- plot_ly(datshr, x = ~ elev, y = ~ cadmium) %>%
  add_markers() %>% 
  highlight(on = "plotly_selected", persistent = TRUE)
## We recommend setting `persistent` to `FALSE` (the default) because persistent selection mode can now be used by holding the shift key (while triggering the `on` event).
# map
meuse_map <- plot_ly(datshr) %>%
  highlight(persistent = T, dynamic = T)
## Adding more colors to the selection color palette.
## We recommend setting `persistent` to `FALSE` (the default) because persistent selection mode can now be used by holding the shift key (while triggering the `on` event).
# combine scatterplot and map
bscols(
  meuse_sct, 
  meuse_map
)
## Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.

Map view is awesome for a basic base maps

library(mapview)
#example point
mapview(breweries)
#using an example of our stuff earlier
mapview(states, zcol= "area")